<template>
  <div id="addaddr">
    <van-address-edit
      :area-list="areaList"
      show-delete
      show-set-default
      :address-info="addrobj"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
    />
  </div>
</template>

<script>
import { Toast } from "vant";
import { areaList } from "@vant/area-data";
import { AddAddr, EditAddr, RemAddr } from "../util/request/api";
export default {
  data() {
    return {
      areaList
    };
  },
  props: ["addrobj"],
  methods: {
    onSave(content) {
      let addrinfo = {
        uid: this.uid,
        username: content.name,
        userphone: content.tel,
        regioncode: content.areaCode,
        location: content.province + content.city + content.county,
        useraddress: content.addressDetail,
        status: content.isDefault ? 1 : 0
      };
      if (this.$route.path == "/addaddr") {
        AddAddr(addrinfo)
          .then(res => {
            if (res.code == 200) {
              Toast("地址保存成功");
              this.$router.replace("/addr");
            } else {
              Toast(res.msg);
            }
          })
          .catch(err => {
            console.log(err);
          });
      } else {
        addrinfo.id = this.addrobj.id;
        EditAddr(addrinfo)
          .then(res => {
            if (res.code == 200) {
              Toast("地址保存成功");
              this.$router.replace("/addr");
            } else {
              Toast(res.msg);
            }
          })
          .catch(err => {
            console.log(err);
          });
      }
    },
    onDelete(item) {
      if (this.$route.path == "/addaddr") {
        return false;
      }
      RemAddr(item.id)
        .then(res => {
          Toast(res.msg);
          this.$router.replace("/addr");
        })
        .catch(err => {
          console.log(err);
        });
    }
  },
  mounted() {},
  computed: {
    uid: function() {
      return JSON.parse(sessionStorage.getItem("user")).uid;
    }
  }
};
</script>

<style scoped>
.van-switch {
  font-size: 0.26rem ;
}

</style>